package com.sun.alen.controller;


import com.sun.alen.entity.Admin;
import com.sun.alen.entity.Result;
import com.sun.alen.service.AdminService;
import com.sun.alen.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/admin")
@Slf4j
public class AdminController {

    @Autowired
    private AdminService adminService;

    /**
     * 管理员登录
     * @param admin
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody Admin admin){
        log.info("{}",admin);
        Admin admin1=adminService.login(admin);
        if (admin1!=null){
            //登录成功，生成令牌，下发令牌
            Map<String,Object> claims=new HashMap<>();
            claims.put("admin",admin1);
            String jwt= JwtUtils.generateJwt(claims);//jwt包含当前登录的管理员信息
            return Result.success(jwt);
        }else {
            return Result.error("登录失败");
        }
    }

    /**
     * 获取jwt令牌
     * @param authorizationHeader
     * @return
     */
    @GetMapping("/jwt")
    public Result jwt(@RequestHeader("Authorization")String authorizationHeader){
        String token= authorizationHeader.substring("Bearer".length()).trim();
        //验证JWT令牌
        Claims claims= JwtUtils.parseJWT(token);
        return Result.success(claims);
    }

    /**
     * 修改图片
     * @param admin
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Admin admin){
        boolean flag=adminService.update(admin);
        Admin admin1=adminService.selectById(admin.getAdminId());
        if (flag){
            return Result.success(admin1);
        }else {
            return Result.error("修改失败");
        }
    }

    /**
     * 修改密码
     * @param admin
     * @return
     */
    @PutMapping("/password")
    public Result updatePassword(@RequestBody Admin admin){
        boolean flag=adminService.updatePassword(admin);
        Admin admin1=adminService.selectById(admin.getAdminId());
        if (flag){
            return Result.success(admin1);

        }else {
            return Result.error("修改失败");
        }
    }

    @PostMapping("/phone")
    public Result send(@RequestBody String phone){
        int flag=adminService.send(phone);
        if(flag==0){
            return Result.success();
        }else if(flag==1){
            return Result.error("手机号不可为空");
        }else {
            return Result.error("发送失败");
        }
    }

    @PutMapping("/repass")
    public Result repass(@RequestParam("phone") String phone,
                         @RequestParam("pass") String pass,
                         @RequestParam("verify") String verify) {
        log.info("{}", phone);
        log.info("{}", pass);
        log.info("{}", verify);
        int flag = adminService.repass(phone, pass, verify);
        if (flag == 0) {
            return Result.success();
        } else if (flag == 1) {
            return Result.error("验证码错误");
        } else {
            return Result.error("重置失败");
        }
    }
}
